package com.dai.spec.gxk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dai.spec.common.constant.RoleConstants;
import com.dai.spec.common.core.domain.entity.SysUser;
import com.dai.spec.common.exception.base.BaseException;
import com.dai.spec.common.utils.SecurityUtils;
import com.dai.spec.common.utils.bean.BeanUtils;
import com.dai.spec.gxk.domain.Course;
import com.dai.spec.gxk.domain.Teacher;
import com.dai.spec.gxk.mapper.CourseMapper;
import com.dai.spec.gxk.mapper.MyCourseMapper;
import com.dai.spec.gxk.mapper.TeacherMapper;
import com.dai.spec.gxk.request.MyCoursePageRequest;
import com.dai.spec.gxk.service.MyCourseService;
import com.dai.spec.gxk.vo.MyCourseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyCourseServiceImpl implements MyCourseService {

    private TeacherMapper teacherMapper;

    private MyCourseMapper myCourseMapper;
    private CourseMapper courseMapper;


    public MyCourseServiceImpl(TeacherMapper teacherMapper, MyCourseMapper myCourseMapper, CourseMapper courseMapper) {
        this.teacherMapper = teacherMapper;
        this.myCourseMapper = myCourseMapper;
        this.courseMapper = courseMapper;
    }

    /**
     * 分页查询我任教的课程列表
     *
     * @param pageRequest
     * @return
     */
    @Override
    public Page<MyCourseVO> queryMyCourseList(MyCoursePageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        if (!RoleConstants.TEACHER.equals(roleId)) {
            throw new BaseException("当前登录用户不为教师，无权限！");
        }
        String jobNumber = user.getUserName();
        if (StringUtils.isNotBlank(jobNumber)) {
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teacher::getJobNumber, jobNumber);
            Teacher teacher = teacherMapper.selectOne(queryWrapper);
            if (null != teacher) {
                String teacherName = teacher.getName();
                pageRequest.setTeacherName(teacherName);
                Page<MyCourseVO> myCourseVOS = myCourseMapper.queryMyCourseList(pageRequest);
                return myCourseVOS;
            }
        }
        return null;
    }

    /**
     * 查询我任教的课程列表
     *
     * @param pageRequest
     * @return
     */
    @Override
    public List<MyCourseVO> queryList(MyCoursePageRequest pageRequest) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        if (!RoleConstants.TEACHER.equals(roleId)) {
            throw new BaseException("当前登录用户不为教师，无权限！");
        }
        String jobNumber = user.getUserName();
        if (StringUtils.isNotBlank(jobNumber)) {
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teacher::getJobNumber, jobNumber);
            Teacher teacher = teacherMapper.selectOne(queryWrapper);
            List<MyCourseVO> myCourseVOList = new ArrayList<>();
            if (null != teacher) {
                String teacherName = teacher.getName();
                LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Course::getTeacherName, teacherName);
                List<Course> courseList = courseMapper.selectList(lambdaQueryWrapper);
                if (!courseList.isEmpty() && courseList.size() > 0) {
                    for (Course course : courseList) {
                        MyCourseVO myCourseVO = new MyCourseVO();
                        BeanUtils.copyBeanProp(myCourseVO, course);
                        myCourseVOList.add(myCourseVO);
                    }
                }
                return myCourseVOList;
            }
        }

        return null;
    }
}
